#include "Broadcast.h"

Broadcast::Broadcast()
{
}

Broadcast::~Broadcast()
{
}

void Broadcast::Push(ALARM_INFO& info)
{
	std::unique_lock< std::mutex> _guard(m_lock);
	if (m_queue.size() <= 0)
	{
		m_queue.push(info);
		return;
	}

	while (!m_queue.empty())
	{
		ALARM_INFO& old = m_queue.front();
		if (DateTime::Diff(old.alarmTime, info.alarmTime) <= m_cache_sec)
		{
			break;
		}
		m_queue.pop();
	}
	m_queue.push(info);
}

ALARM_LIST Broadcast::Latest(DateTime lastTime)
{
	ALARM_LIST list = {};
	ALARM_CACHE cache;
	{
		std::unique_lock< std::mutex> _guard(m_lock);
		cache = m_queue;
	}
	while (!cache.empty())
	{
		ALARM_INFO& old = cache.front();
		if (DateTime::Diff(lastTime, old.alarmTime) >= 0)
		{
			list.push_back(old);
		}
		cache.pop();
	}
	return list;
}